package com.itic.mall.mobile.special;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample;
import com.itic.mall.system.market.marketadvert.persistence.model.MarketAdvert;
import com.itic.mall.system.market.marketadvert.persistence.model.MarketAdvertExample;
import com.itic.mall.system.market.marketadvert.service.MarketAdvertService;
import com.itic.mall.system.market.marketspecial.persistence.model.MarketSpecial;
import com.itic.mall.system.market.marketspecial.persistence.model.MarketSpecialExample;
import com.itic.mall.system.market.marketspecial.service.MarketSpecialService;

/**
 * 特卖接口
 * @author 王文鑫
 * @date 2017年12月26日 上午9:49:35
 */
@Controller
@RequestMapping(value="/mobile/special")
public class specialInterface  extends BaseMobileInterface{

	@Autowired 
	private MarketSpecialService marketSpecialService;
	
	@Autowired
	private MarketAdvertService marketAdvertService;
	
	// 返回属性黑名单
    private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime", "orgCode","isShow" };
    
    /**
     * 获取特卖全部时间
     * @author 王文鑫
     * @date 2017年12月26日 下午5:18:28
     * @param request
     * @param response
     * @param spuPlatform
     * @return
     */
   @RequestMapping(value="/getspecialtime")
   @ResponseBody
   public Map<String,Object> getspecialtime( HttpServletRequest request,HttpServletResponse response,String spuPlatform){
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	   	String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	   	try{
	   		MarketSpecialExample example = new MarketSpecialExample();
	   		
	   		example.setOrderByClause("SPECIAL_TIME ASC"); //特卖时间排序
	   		com.itic.mall.system.market.marketspecial.persistence.model.MarketSpecialExample.Criteria criteria = example.createCriteria();
	   		
	   		criteria.andIsEffectiveEqualTo(DBConst.TRUE);//是否有效
	   		criteria.andSpuPlatformEqualTo(spuPlatform); //002  京东分类
	   		
	   		List<MarketSpecial> list = marketSpecialService.list(example);
	   		
	   		
	   		List<MarketSpecial> list1 = new ArrayList<MarketSpecial>();
	   		Date nowtime = new Date(); //当前时间
	   		String specialStartTimeStr = null; //特卖开始时间
	   		String specialEndTimeStr = null; //特卖结束时间
	   		String nowtimeStr = DateHelper.formatDateTime(nowtime); //转换当前时间为str
	   		
	   		for (int i = 0; i < list.size(); i++) {
	   			
	   			 specialStartTimeStr = DateHelper.formatDateTime(list.get(i).getSpecialTime());
	   			 specialEndTimeStr = DateHelper.formatDateTime(list.get(i).getSpecialEndTime());
	   			
	   			if(DateHelper.isInStartTimeAndStopTime(nowtimeStr, specialStartTimeStr, specialEndTimeStr)){
	   				list.get(i).setNowTime(nowtime); //设置当前时间
	   				list.get(i).setIsSpecial(1);//是否特卖中
	   			}else if(DateHelper.isGreater(nowtimeStr, specialStartTimeStr)){
	   				//如果都是已开抢，最后一个是开抢中
	   				if( (i+1) == list.size()){ 
		   				list.get(i).setNowTime(nowtime); //设置当前时间
		   				list.get(i).setIsSpecial(1);//是否特卖中
		   			}else{
		   				list.get(i).setIsSpecial(0);//已开抢
		   			}
	   			}else if(DateHelper.isGreater(specialStartTimeStr, nowtimeStr)){
	   				list.get(i).setIsSpecial(2);//未开抢
	   			}
	   			
	   			list1.add(list.get(i)); //添加返回list
			}
	   		
	   		resultMap.put(DATA, ValueHelper.tranList2MapList(list1, new String[] { "note","reorder","categoryCode","createUser", "isEffective", "updateTime", "orgCode","isVisible","createTime"}));
	   		errorCode = SUCCESS_ERROR_CODE;
	       	errorMsg = SUCCESS_ERROR_MSG;
	   	}catch(Exception e){
	   		errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("marketSpecialService", "getspecialtime", e.toString(),"获取特卖时间",e);
	   	}
	   	resultMap.put(CODE, errorCode);
	   	resultMap.put(MSG, errorMsg);
	   	isCrrossDomain(response);
	   	return resultMap;
   }
   
    /**
     * 获取广告位
     * @author 王文鑫
     * @date 2017年12月26日 上午11:05:47
     * @param request
     * @param response
     * @param address
     * @return
     */
    @RequestMapping(value = "/advertlist")
	@ResponseBody
	public Map<String, Object> advertlist(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();

			MarketAdvertExample example = new MarketAdvertExample();
			example.setOrderByClause("REORDER ASC");
			com.itic.mall.system.market.marketadvert.persistence.model.MarketAdvertExample.Criteria criteria = example
					.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andIsVisibleEqualTo(DBConst.TRUE);
			
			criteria.andAddressEqualTo("special");
			criteria.andTypeEqualTo("special");

			List<MarketAdvert> list = marketAdvertService.list(example);// 数据list
			
			dataMap.put("list", ValueHelper.tranList2MapList(list, ignoreProperties));

			resultMap.put(DATA, dataMap);

			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("goodsSpuService", "speciallist", e.toString(), "特卖商品list", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
    
   
   	/**
   	 * 特卖商品list
   	 * @author 王文鑫
   	 * @date 2017年12月26日 上午11:00:29
   	 * @param request
   	 * @param response
   	 * @param type
   	 * @param keyword
   	 * @param specialId
   	 * @param orderType
   	 * @return
   	 */
    @RequestMapping(value = "/speciallist")
	@ResponseBody
	public Map<String, Object> speciallist(HttpServletRequest request, HttpServletResponse response, String type,
			String keyword, String specialId, String orderType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();

			GoodsSpuExample example = new GoodsSpuExample();
			if (StringHelper.isNotBlank(orderType)) {
				if ("priceDesc".equals(orderType)) {
					example.setOrderByClause("PRICE DESC");
				}
				if ("priceAsc".equals(orderType)) {
					example.setOrderByClause("PRICE ASC");
				}
				if ("commisionDesc".equals(orderType)) {
					example.setOrderByClause("MARKET_PROFIT ASC");
				}
				if ("commisionAsc".equals(orderType)) {
					example.setOrderByClause("MARKET_PROFIT ASC");
				}
			} else {
				example.setOrderByClause("CREATE_TIME DESC");
			}

			com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example
					.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
			criteria.andIsShowEqualTo(DBConst.TRUE);// 是否可见

			criteria.andIsSpecialEqualTo("1"); // 是否特卖

			//查询条件特卖id
			criteria.andSpecialIdEqualTo(specialId);
			
			criteria.andSpuPlatformEqualTo(type);

			this.handlePageQueryDefault(request, example);// 分页
			int count = goodsSpuService.count(example);// 总条数
			List<GoodsSpu> list = goodsSpuService.list(example);// 数据list

			dataMap.put("total", count);
			dataMap.put("isMore", isMore(request, count));
			dataMap.put("list",
					ValueHelper.tranList2MapList(list,
							new String[] { "totalFeedback", "isServenBack", "detailImageUrls", "reorder", "goodsSource",
									"orderHot", "goodsSubtitle", "orderJingping", "marketDate",
									"totalGoodFeedback", "banners", "isShow", "companyId", "note", "reorder",
									"categoryId", "createUser", "isEffective", "updateTime", "orgCode", "isVisible",
									"createTime", "goodsTitle", "brandId", "spuNum", "isFreePost","isJingping","isHot","isSpecial"}));
			resultMap.put(DATA, dataMap);

			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("goodsSpuService", "speciallist", e.toString(), "特卖商品list", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
}
